package com.example.goodpet.my.activity;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.goodpet.R;
import com.example.goodpet.my.adapter.MyFensiAdapter;
import com.example.goodpet.bean.ResponseBody;
import com.example.goodpet.bean.User;
import com.example.goodpet.common.EndlessRecyclerOnScrollListener;
import com.example.goodpet.common.LoadMoreWrapper;
import com.example.goodpet.service.UserService;
import com.example.goodpet.utils.ActivityManager;
import com.example.goodpet.utils.MyApplication;
import com.example.goodpet.utils.RetrofitSingleton;
import com.example.goodpet.utils.UIUtils;
import com.zackratos.ultimatebarx.ultimatebarx.java.UltimateBarX;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MyFensiActivity extends FragmentActivity {
    private  final String TAG = this.getClass().getSimpleName();
    private TextView cmTitleText;
    private ImageView cmTitleLeft;
    private TextView cmTitleRight;

    private RecyclerView recyclerView;

    private List<User> data;

    private Retrofit retrofit;
    private UserService userService;
    private long userId;

    LoadMoreWrapper loadMoreWrapper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_fensi);
        UltimateBarX.statusBarOnly(this)
                .fitWindow(true)
                .colorRes(R.color.ic_back_background)
                .light(true)
                .apply();
        ActivityManager.getInstance().addActivity(this);

        cmTitleLeft=findViewById(R.id.cmTitleLeft);
        cmTitleText=findViewById(R.id.cmTitleText);
        cmTitleRight=findViewById(R.id.cmTitleRight);
        recyclerView=findViewById(R.id.recyclerView);

        userId=getIntent().getLongExtra("userId",-1);

        initListener();
        initTitle();
    }
    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        retrofit= RetrofitSingleton.getRetrofitNetConfig();
        userService=retrofit.create(UserService.class);
        userService.getMyFensi(userId,1).enqueue(new Callback<ResponseBody<List<User>>>() {
            @Override
            public void onResponse(Call<ResponseBody<List<User>>> call, Response<ResponseBody<List<User>>> response) {
                ResponseBody<List<User>> body = response.body();
                if (body!=null){
                    if (body.getCode()==200){
                        data=body.getData();
                        //Log.d(TAG, "onResponse: "+);
                        MyFensiAdapter myFensiAdapter = new MyFensiAdapter(data,MyFensiActivity.this);
                        loadMoreWrapper = new LoadMoreWrapper(myFensiAdapter);
                        recyclerView.setAdapter(loadMoreWrapper);
                        recyclerView.setLayoutManager(new GridLayoutManager(MyFensiActivity.this,1));
                        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
                            @Override
                            public void onLoadMore() {
                                loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING);
                                userService.getMyFensi(MyApplication.user.getId(), ++loadMoreWrapper.page).enqueue(new Callback<ResponseBody<List<User>>>() {
                                    @Override
                                    public void onResponse(Call<ResponseBody<List<User>>> call, Response<ResponseBody<List<User>>> response) {
                                        ResponseBody<List<User>> body1 = response.body();
                                        //Log.d(TAG, "onResponse: "+ body);
                                        if (body1.getCode()==200){
                                            List<User> data1 = body1.getData();
                                            data.addAll(data1);
                                            loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_COMPLETE);
                                        }else if (body1.getCode()==404){
                                            loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_END);
                                        }

                                    }

                                    @Override
                                    public void onFailure(Call<ResponseBody<List<User>>> call, Throwable t) {
                                        t.printStackTrace();
                                        UIUtils.toast("连接服务器失败",false);
                                    }
                                });
                            }
                        });

                    }else if (body.getCode()==404){
                        if (userId!=MyApplication.user.getId()){
                            UIUtils.toast("数据为空",false);
                        }else UIUtils.toast("您还没有粉丝哦！",false);
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody<List<User>>> call, Throwable t) {
                t.printStackTrace();
                UIUtils.toast("连接服务器失败",false);
            }
        });
    }

    private void initTitle() {
        cmTitleText.setText("粉丝");
        cmTitleRight.setVisibility(View.INVISIBLE);
    }

    private void initListener() {
        cmTitleLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityManager.getInstance().removeCurrent();
            }
        });
    }
}